import Vue from 'vue'
import store from '../store/index.js';


// 图片格式处理
export function imageUrl(fileUrl) {
	if (fileUrl) {
		if (fileUrl.indexOf('data:image') > -1) {
			return fileUrl;
		} else {
			if (fileUrl.indexOf('https://') > -1 || fileUrl.indexOf('http://') > -1) {
				return fileUrl;
			} else {
				const cdnurl = store.state.commInitInfo.uploaddata.cdnurl;
				return cdnurl + fileUrl;
			}
		}
	} else {
		return 'https://test-zyxx-img.oss-cn-shenzhen.aliyuncs.com/uploads/20241202/7847d6a839a54d2700bddad75d583007.png'
	}
}

// 时间格式处理
export function formatTimestamp(timestamp) {
	const date = new Date(timestamp * 1000);
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');
	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 金额格式处理
export function formatAmount(num, decimalPlaces = 2) {
	return num.toFixed(decimalPlaces).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

// 金额格式w,k
export function formatAmountWk(num) {
	if (num >= 10000) {
		const integerPart = Math.floor(num / 10000);
		const decimalPart = num % 10000;
		const decimalStr = decimalPart.toString().padStart(4, '0');
		return `${integerPart}.${decimalStr.slice(0, 2)}w`;
	} else if (num >= 1000) {
		const integerPart = Math.floor(num / 1000);
		const decimalPart = num % 1000;
		const decimalStr = decimalPart.toString().padStart(3, '0');
		return `${integerPart}.${decimalStr.slice(0, 2)}k`;
	}
	return num;
}

// 手机号码校验
export function validatePhoneNumber(phoneNumber) {
	const phoneRegex = /^1[3-9]\d{9}$/;
	return phoneRegex.test(phoneNumber);
}

// 校验身份证号码
export function validateIDCard(idCard) {
	if (idCard.length !== 18) {
		return false;
	}
	// 校验地址码
	const addressCode = idCard.slice(0, 6);
	if (!/^\d{6}$/.test(addressCode)) {
		return false;
	}
	// 校验出生日期码
	const birthDateCode = idCard.slice(6, 14);
	const birthDate = new Date(birthDateCode.slice(0, 4), birthDateCode.slice(4, 6) - 1, birthDateCode.slice(6));
	if (birthDate.getFullYear() !== parseInt(birthDateCode.slice(0, 4)) ||
		birthDate.getMonth() + 1 !== parseInt(birthDateCode.slice(4, 6)) ||
		birthDate.getDate() !== parseInt(birthDateCode.slice(6))) {
		return false;
	}
	// 校验顺序码
	const sequenceCode = idCard.slice(14, 17);
	if (!/^\d{3}$/.test(sequenceCode)) {
		return false;
	}
	// 校验校验码
	const coefficient = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
	const lastNumber = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
	let sum = 0;
	for (let i = 0; i < 17; i++) {
		sum += parseInt(idCard[i]) * coefficient[i];
	}
	const remainder = sum % 11;
	if (lastNumber[remainder] !== idCard[17]) {
		return false;
	}
	return true;
}

// 单位税号校验
export function validateTaxID(taxID) {
	if (taxID.length !== 18) {
		return false;
	}
	const weightFactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
	let sum = 0;
	for (let i = 0; i < 17; i++) {
		let charValue;
		if (taxID[i].match(/[0 - 9]/)) {
			charValue = parseInt(taxID[i]);
		} else {
			charValue = taxID[i].charCodeAt(0) - 55;
		}
		sum += charValue * weightFactors[i];
	}
	let checkCode;
	let remainder = 31 - sum % 31;
	if (remainder === 31) {
		checkCode = 0;
	} else if (remainder === 0) {
		checkCode = 31;
	} else {
		checkCode = remainder;
	}
	let expectedCheckCode;
	if (taxID[17].match(/[0 - 9]/)) {
		expectedCheckCode = parseInt(taxID[17]);
	} else {
		expectedCheckCode = taxID[17].charCodeAt(0) - 55;
	}
	return checkCode === expectedCheckCode;
}

// 邮箱校验
export function validateEmail(email) {
	const emailRegex = /^[a - zA - Z0 - 9_. -]+@[a - zA - Z0 - 9 -]+(\.[a - zA - Z0 - 9 -]+)*$/;
	return emailRegex.test(email);
}

// 银行卡号码校验
export function validateBankAccount(bankAccount) {
	if (!/^\d{13,19}$/.test(bankAccount)) {
		return false;
	}
	let sum = 0;
	let digits = bankAccount.split('').reverse();
	for (let i = 0; i < digits.length; i++) {
		let digit = parseInt(digits[i]);
		if (i % 2 !== 0) {
			digit *= 2;
			if (digit > 9) {
				digit -= 9;
			}
		}
		sum += digit;
	}
	return sum % 10 === 0;
}

// 银行卡号码加密
export function maskBankAccountRegex(accountNumber) {
	return accountNumber.replace(/^(\d{4}).*(\d{4})$/g, '$1' + '*'.repeat(arguments[0].length - 8) + '$2');
}